package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.TestSoulApi;
import com.tanhua.dubbo.server.pojo.*;
import com.tanhua.server.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Ling
 * @Title: null.java
 * @Package com.tanhua.server.service
 * @Description:
 * @date 2021/9/14 13:56
 */
@Service
public class TestSoulService {

    @Reference(version = "1.0.0")
    private TestSoulApi testSoulApi;

    @Autowired
    private UserInfoService userInfoService;

    public List<QuestionnaireVo> queryQuestionnaireList() {
        List<QuestionnaireVo> list = new ArrayList<>();

        User user = UserThreadLocal.get();
        if (ObjectUtil.isEmpty(user)) {
            return ListUtil.empty();
        }

        List<TestSoulQuestionnaire> testSoulQuestionnaires = testSoulApi.queryTestSoulQuestionnaireAll();
        for (TestSoulQuestionnaire testSoulQuestionnaire : testSoulQuestionnaires) {
            QuestionnaireVo questionnaireVo = new QuestionnaireVo();
            questionnaireVo.setId(testSoulQuestionnaire.getQuestionnaireId());
            questionnaireVo.setName(testSoulQuestionnaire.getName());
            questionnaireVo.setCover(testSoulQuestionnaire.getCover());
            questionnaireVo.setLevel(testSoulQuestionnaire.getLevel());
            questionnaireVo.setStar(testSoulQuestionnaire.getStar());

            List<Integer> questionIds = testSoulQuestionnaire.getQuestionId();
            List<QuestionsVo> listQ = new ArrayList<>();
            List<TestSoulQuestions> testSoulQuestions = testSoulApi.queryTestSoulQuestionsByIds(questionIds);
            for (TestSoulQuestions testSoulQuestion : testSoulQuestions) {
                QuestionsVo questionsVo = new QuestionsVo();
                questionsVo.setId(testSoulQuestion.getQuestionId());
                questionsVo.setQuestion(testSoulQuestion.getQuestionText());


                List<OptionsVo> listO = new ArrayList<>();
                List<String> optionIds = testSoulQuestion.getOptionId();

                List<TestSoulOption> testSoulOptions = testSoulApi.queryTestSoulOptionByIds(optionIds);
                for (TestSoulOption testSoulOption : testSoulOptions) {
                    OptionsVo optionsVo = new OptionsVo();
                    optionsVo.setId(testSoulOption.getOptionId().split("_")[1]);
                    optionsVo.setOption(testSoulOption.getOptionText());
                    listO.add(optionsVo);
                }
                questionsVo.setOptions(listO);
                listQ.add(questionsVo);
            }
            questionnaireVo.setQuestions(listQ);


            questionnaireVo.setIsLock(1);
            TestSoulRelations testSoulRelations = testSoulApi.queryTestSoulRelationsByUserID(user.getId());
//            TestSoulRelations testSoulRelations = testSoulApi.queryTestSoulRelationsByUserID(1L);
            if (ObjectUtil.isEmpty(testSoulQuestionnaire)) {
                if (questionnaireVo.getId() == 1) {
                    questionnaireVo.setIsLock(0);
                }
                list.add(questionnaireVo);
                continue;
            }

//            Integer scoreQ1 = testSoulRelations.getScoreQ1();
//            Integer scoreQ2 = testSoulRelations.getScoreQ2();
//            Integer scoreQ3 = testSoulRelations.getScoreQ3();


            list.add(questionnaireVo);
        }
//        list = ifUnlock(list);
        list = ifUnlock(list, user);

        return list;
    }

    public String postAnswerList(List<AnswersVo> list) {
//        Set<String> strings = map.keySet();
//        for (String string : strings) {
//            System.out.println(string + " " + map.get(string));
//        }
        List<String> ids = new ArrayList<>();
        for (AnswersVo o : list) {
            ids.add(o.getQuestionId() + "_" + o.getOptionId());
        }

        List<TestSoulOption> testSoulOptions = testSoulApi.queryTestSoulOptionByIds(ids);
        Integer score = 0;
        for (TestSoulOption testSoulOption : testSoulOptions) {
            score += testSoulOption.getScore();
        }


        TestSoulResult testSoulResult = testSoulApi.queryTestSoulResultByScore(score);

        return testSoulResult.getResultId().toString();
    }

    public TestSoulReportVo queryReport(Integer id) {
        TestSoulReportVo reportVo = new TestSoulReportVo();

        TestSoulResult testSoulResult = testSoulApi.queryTestSoulResultById(id);
        reportVo.setCover(testSoulResult.getCover());
        reportVo.setConclusion(testSoulResult.getResultText());
        reportVo.setDimensions(testSoulResult.getDimensions());

        User user = UserThreadLocal.get();

        TestSoulRelations testSoulRelations = testSoulApi.queryTestSoulRelationsByUserID(user.getId());

        Integer scoreQ1 = testSoulRelations.getScoreQ1();
        Integer scoreQ2 = testSoulRelations.getScoreQ2();
        Integer scoreQ3 = testSoulRelations.getScoreQ3();
        Set<Long> longs = null;
        if (ObjectUtil.isAllNotEmpty(scoreQ1, scoreQ2, scoreQ3)) {
            longs = testSoulApi.queryUserIdsByScore((scoreQ1 + scoreQ2 + scoreQ3) / 3);
        } else {
            longs = testSoulApi.queryUserIdsByScore(scoreQ1);
        }
        List<UserInfo> userInfos = userInfoService.queryUserInfoByUserIdList(longs);
        List<similarVo> similarVos = new ArrayList<>();

        for (UserInfo userInfo : userInfos) {
            similarVo similarVo = new similarVo();
            similarVo.setId(userInfo.getUserId());
            similarVo.setAvatar(userInfo.getLogo());
            similarVos.add(similarVo);
        }
        reportVo.setSimilarYou(similarVos);
        return reportVo;
    }

//        private List<QuestionnaireVo> ifUnlock(List<QuestionnaireVo> list/*, User user*/) {
    private List<QuestionnaireVo> ifUnlock(List<QuestionnaireVo> list, User user) {
        Long userId = user.getId();
//        Long userId = 1L;
        TestSoulRelations testSoulRelations = testSoulApi.queryTestSoulRelationsByUserID(userId);
        if (ObjectUtil.isEmpty(testSoulRelations)) {
            return list;
        }
        Integer scoreQ1 = testSoulRelations.getScoreQ1();
        Integer scoreQ2 = testSoulRelations.getScoreQ2();
        Integer scoreQ3 = testSoulRelations.getScoreQ3();
        if (!ObjectUtil.isEmpty(scoreQ1)) {
            list.get(0).setReportId(testSoulApi.queryTestSoulResultByScore(scoreQ1).getResultId().toString());
            list.get(1).setIsLock(0);
        }
        if (!ObjectUtil.isEmpty(scoreQ2)) {
            list.get(1).setReportId(testSoulApi.queryTestSoulResultByScore(scoreQ2).getResultId().toString());
            list.get(2).setIsLock(0);
        }
        if (!ObjectUtil.isEmpty(scoreQ3)) {
            list.get(2).setReportId(testSoulApi.queryTestSoulResultByScore(scoreQ3).getResultId().toString());
        }
        return list;
    }
}
